package com.gateon.hw.base.exception;

import com.gateon.hw.base.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created by gongweibing on 2020/4/15.
 */
@Slf4j
@RestControllerAdvice
public class ApiException {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionErrorHandler(Exception e) throws Exception {
        log.error("服务异常",e);
        return Result.fail(110,e.getMessage());
    }


    @ExceptionHandler(value =MethodArgumentNotValidException.class)
    @ResponseBody
    public Result exceptionBindExceptionrHandler(MethodArgumentNotValidException e) throws Exception {
        return Result.validMsg(e.getBindingResult());
    }


}
